/*!
 * Page: pages/setting/Setting
 * License: MIT
 * Created: 2019-10-15 17:00
 */

import { userApi } from '/api/user'
import { getServer } from '/store/mutations'
import { GlobalData } from '/store/state'

Page({
  data: {
    list: [
      // {
      //   title: '黑名单',
      //   page: '/mine/blacklist/blacklist',
      // },
      {
        title: '意见反馈',
        page: '/mine/feedback/feedback',
      },
      {
        title: '关于我们',
        page: '/mine/aboutus/aboutus',
      },
      // {
      //   title: '用户协议',
      //   page: '/mine/aboutus/aboutus?type=user_agreement',
      // },
      {
        title: '隐私政策',
        page: '/mine/aboutus/aboutus?type=privacy_agreement',
      },
      // {
      //   title: '客服',
      //   page: '/mine/customerService/customerService?type=1',
      // },
    ],
  },
  onLoad() {
    // const openAiAppId = '{{OPENAI_APPID}}'
    // if (openAiAppId) {
    //   const items = this.data.list
    //   items.push({
    //     title: '客服',
    //     page: '/mine/customer-service/customer-service',
    //   })
    //   this.setData({
    //     list: items,
    //   })
    // }
  },
  goToPage(e: any) {
    const {
      currentTarget: {
        dataset: {
          page: { page, title },
        },
      },
    } = e

    if (title === '意见反馈' && GlobalData.hasIM && GlobalData.imHelper) {
      // 如果配置了小助手，跳转小助手聊天
      // 当前登录用户的用户信息
      const user = wx.getStorageSync(`${getServer()}/logged`).data || {}
      const requetData = userApi.getUser(GlobalData.imHelper)
      requetData
        .then((domoUser: DomoUser) => {
          wx.navigateTo({
            url: `/pages/news/chatroom/chatroom?username=${JSON.stringify({
              your: GlobalData.imVersion + '' + GlobalData.imHelper,
              myName: GlobalData.imVersion + '' + user.id,
              name: user.name,
              yourname: domoUser.name,
            })}`,
          })
        })
        .catch(() => {
          wx.showToast({
            title: '打开失败',
            icon: 'none',
          })
        })
    } else {
      wx.navigateTo({
        url: page,
      })
    }
  },
})
